package com.jushu.storbox.newfragment;

import android.app.ActionBar.LayoutParams;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;

import com.jushu.storbox.R;
import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.ReceiverConstent;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.view.dialog.PopupMenuWindow;
import com.jushu.storbox.view.resolve.DownloadResolver;
import com.jushu.storbox.view.resolve.TranslateResolver;
import com.jushu.storbox.view.resolve.UploadResolver;
import com.jushu.storbox.view.viewImpl.StyleView;
import com.jushu.storbox.view.viewImpl.StyleView.OnSelectedListener;
import com.kennyc.view.MultiStateView;

public class TranslateFragment extends AbsFrameFragment {

	private final static String TAG = TranslateFragment.class.getSimpleName();

	private PopupMenuWindow popMenuWindow = null;
	TitleViewHolder titleHolder = null;
	StyleView bodyView;
	
	DownloadResolver downloadResolver = null;
	UploadResolver uploadResolver = null;
	TranslateResolver<?,?,?> currentResolver = null;

	String ownerBy = ShareDriveApplication.getInstance().getWnerID();

	@Override
	public void onResume() {
		IntentFilter iFilter = new IntentFilter();
		iFilter.addAction(ReceiverConstent.DOWNLOAD_REFRESH);
		getActivity().registerReceiver(receiver, iFilter);
		super.onResume();
	}

	@Override
	public void onPause() {
		getActivity().unregisterReceiver(receiver);
		super.onPause();
	}

	protected int getTitleResourceId() {
		return R.layout.action_title_translist;
	}

	@Override
	protected int getContentResourceId() {
		return R.layout.action_body_style_two;
	}

	@Override
	protected void onLoadTitleView(View titleView) {
		super.onLoadTitleView(titleView);
		titleHolder = new TitleViewHolder(titleView);
	}

	@Override
	protected void onLoadContentView(View contentView) {
		super.onLoadContentView(contentView);
		downloadResolver = new DownloadResolver(getActivity());
		uploadResolver = new UploadResolver(getActivity());
		((MultiStateView) contentView.findViewById(R.id.multiStateView)).setViewState(MultiStateView.ViewState.CONTENT);
		
		bodyView = (StyleView) contentView.findViewById(R.id.translate_compent);
		bodyView.setOnSelectedListener(new OnSelectedListener() {
			@Override
			public void onLeftItemSelected(View view) {
				currentResolver = uploadResolver;
				currentResolver.binddingAdapter();
			}

			@Override
			public void onRightItemSelected(View view) {
				currentResolver = downloadResolver;
				currentResolver.binddingAdapter();
			}
		});

		bodyView.getLeftTextView().setText(R.string.action_translate_upload);
		bodyView.getRightTextView().setText(R.string.action_translate_download);
		
		downloadResolver.initComponent(bodyView.getListView());
		uploadResolver.initComponent(bodyView.getListView());
		
		currentResolver = uploadResolver;
		currentResolver.binddingAdapter();
	}
	
	@Override
	protected void startInitConpentData() {
	}

	private class TitleViewHolder {
		View titleContainer;

		public TitleViewHolder(View container) {
			titleContainer = container;

			View imgMore =  titleContainer.findViewById(R.id.file_action_title_moreOperation);
			imgMore.setOnClickListener(new OnClickListener() {
				public void onClick(View view) {
					if (popMenuWindow == null) {
						initPopMenuWindow();
					}

					int[] location = new int[2];
					view.getLocationOnScreen(location);
					// -5 -- background border is 5dp
					popMenuWindow.showAtLocation(titleContainer, Gravity.NO_GRAVITY, location[0] - popMenuWindow.getWidth() + view.getWidth(), location[1]
							+ titleContainer.getHeight() - PublicTools.dipTopx(getActivity(), 5));
				}
			});
		}

		private void initPopMenuWindow() {
			popMenuWindow = new PopupMenuWindow(getActivity(), (int) getActivity().getResources().getDimension(R.dimen.item_popup_more_window_width_l),
					LayoutParams.WRAP_CONTENT, R.layout.action_title_translist_dropdown) {
				protected void onInflaterFinished(View contentView) {
					View trans_layout_start = contentView.findViewById(R.id.trans_layout_start);
					trans_layout_start.setOnClickListener(new OnClickListener() {
						public void onClick(View arg0) {
							dismiss();
							if (currentResolver != null) {
								currentResolver.startAllTasks();
							}
						}
					});
					View trans_layout_stop = contentView.findViewById(R.id.trans_layout_stop);
					trans_layout_stop.setOnClickListener(new OnClickListener() {
						public void onClick(View arg0) {
							dismiss();
							if (currentResolver != null) {
								currentResolver.stopAllTasks();
							}
						}
					});
					View trans_layout_delete = contentView.findViewById(R.id.trans_layout_delete);
					trans_layout_delete.setOnClickListener(new OnClickListener() {
						public void onClick(View arg0) {
							dismiss();
							if (currentResolver != null) {
								currentResolver.deleteAllTasks();
							}
						}
					});
				}
			};
		}
	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
//			if (action.equals(ReceiverConstent.DOWNLOAD_REFRESH)) {
//				if (currentResolver instanceof DownloadAdapter) {
//					displayDownloadList();
//				}
//			} else if (currentAdapter instanceof UploadAdapter) {
//				displayUploadList();
//			}
		}
	};

}
